package org.jeecg.modules.workflow.convert.definition;

import com.fasterxml.jackson.core.type.TypeReference;
import org.flowable.engine.repository.Model;
import org.flowable.engine.repository.ProcessDefinition;
import org.jeecg.common.util.json.JsonUtils;
import org.jeecg.common.workflow.vo.definition.model.BpmModeImportReqVO;
import org.jeecg.common.workflow.vo.definition.model.BpmModelBaseVO;
import org.jeecg.common.workflow.vo.definition.model.BpmModelCreateReqVO;
import org.jeecg.common.workflow.vo.definition.model.BpmModelPageItemRespVO;
import org.jeecg.modules.workflow.entity.BpmForm;
import org.jeecg.modules.workflow.service.definition.dto.BpmModelMetaInfoRespDTO;
import org.jeecg.modules.workflow.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 流程模型 Convert
 *
 * @author yunlongn
 */
@Service
public class BpmModelConvertImpl implements BpmModelConvert{
    private static final TypeReference<List<String>> typeReference = new TypeReference<List<String>>(){};


    @Override
    public void copyTo(BpmModelMetaInfoRespDTO from, BpmModelBaseVO to) {
        if ( from == null ) {
            return;
        }

        to.setDescription( from.getDescription() );
        to.setFormType( from.getFormType() );
        to.setFormId( from.getFormId() );
        to.setFormCustomCreatePath( from.getFormCustomCreatePath() );
        to.setFormCustomViewPath( from.getFormCustomViewPath() );
        to.setFormCanReturn(from.getFormCanReturn());
    }

    @Override
    public void copyTo(BpmModelMetaInfoRespDTO from, BpmProcessDefinitionCreateReqDTO to) {
        if ( from == null ) {
            return;
        }

        to.setDescription( from.getDescription() );
        to.setFormType( from.getFormType() );
        to.setFormId( from.getFormId() );
        to.setFormCustomCreatePath( from.getFormCustomCreatePath() );
        to.setFormCustomViewPath( from.getFormCustomViewPath() );
        to.setFormCanReturn(from.getFormCanReturn());
    }

    @Override
    public BpmModelPageItemRespVO.ProcessDefinition convert(ProcessDefinition bean) {
        if ( bean == null ) {
            return null;
        }

        BpmModelPageItemRespVO.ProcessDefinition processDefinition = new BpmModelPageItemRespVO.ProcessDefinition();

        processDefinition.setId( bean.getId() );
        processDefinition.setVersion( bean.getVersion() );

        return processDefinition;
    }

    @Override
    public BpmModelCreateReqVO convert(BpmModeImportReqVO bean) {
        if ( bean == null ) {
            return null;
        }

        BpmModelCreateReqVO bpmModelCreateReqVO = new BpmModelCreateReqVO();

        bpmModelCreateReqVO.setKey( bean.getKey() );
        bpmModelCreateReqVO.setName( bean.getName() );
        bpmModelCreateReqVO.setDescription( bean.getDescription() );

        return bpmModelCreateReqVO;
    }

    public BpmProcessDefinitionCreateReqDTO convert2(Model model, BpmForm form) {
        BpmProcessDefinitionCreateReqDTO createReqDTO = new BpmProcessDefinitionCreateReqDTO();
        createReqDTO.setModelId(model.getId());
        createReqDTO.setName(model.getName());
        createReqDTO.setKey(model.getKey());
        createReqDTO.setCategory(model.getCategory());
        BpmModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoRespDTO.class);
        // metaInfo
        copyTo(metaInfo, createReqDTO);
        // form
        if (form != null) {
            createReqDTO.setFormConf(form.getConf());
            createReqDTO.setFormFields(JsonUtils.parseObject(form.getFields(), typeReference));
        }
        return createReqDTO;
    }
}
